package com.huacai.controller;

import com.huacai.domain.Comment;
import com.huacai.service.ICommentService;
import com.huacai.common.annotation.Log;
import com.huacai.common.core.controller.BaseController;
import com.huacai.common.core.domain.AjaxResult;
import com.huacai.common.core.page.TableDataInfo;
import com.huacai.common.enums.BusinessType;
import com.huacai.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;

/**
 * 评论Controller
 *
 * @author lundao
 * @date 2025-09-14
 */
@RestController
@RequestMapping("/huacai/comment")
public class CommentController extends BaseController
{
    @Autowired
    private ICommentService commentService;

    /**
     * 查询评论列表
     */

    @GetMapping("/list")
    public TableDataInfo list(Comment comment)
    {
        startPage();
        List<Comment> list = commentService.selectCommentList(comment);
        return getDataTable(list);
    }

    /**
     * 导出评论列表
     */

    @Log(title = "评论", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Comment comment)
    {
        List<Comment> list = commentService.selectCommentList(comment);
        ExcelUtil<Comment> util = new ExcelUtil<Comment>(Comment.class);
        util.exportExcel(response, list, "评论数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<Comment> util = new ExcelUtil<Comment>(Comment.class);
        util.importTemplateExcel(response, "评论数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "评论", businessType = BusinessType.IMPORT)

    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<Comment> util = new ExcelUtil<Comment>(Comment.class);
        InputStream inputStream = file.getInputStream();
        List<Comment> list = util.importExcel(inputStream );
        inputStream.close();
        int count = commentService.batchInsertComment(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取评论详细信息
     */

    @GetMapping(value = "/{commentId}")
    public AjaxResult getInfo(@PathVariable("commentId") String commentId)
    {
        return success(commentService.selectCommentByCommentId(commentId));
    }

    /**
     * 新增评论
     */

    @Log(title = "评论", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Comment comment)
    {
        return toAjax(commentService.insertComment(comment));
    }

    /**
     * 修改评论
     */

    @Log(title = "评论", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Comment comment)
    {
        return toAjax(commentService.updateComment(comment));
    }

    /**
     * 删除评论
     */

    @Log(title = "评论", businessType = BusinessType.DELETE)
	@DeleteMapping("/{commentIds}")
    public AjaxResult remove(@PathVariable String[] commentIds)
    {
        return toAjax(commentService.deleteCommentByCommentIds(commentIds));
    }

    /**
     * 根据博客ID获取评论列表
     */
    @GetMapping("/selectCommentByBlogId/{blogId}")
    public AjaxResult selectCommentByBlogId(@PathVariable("blogId") String blogId) {
        List<Comment> list = commentService.selectCommentByBlogId(blogId);
        return success(list);
    }
}
